package edu.metrostate.capstonediner;

import javax.microedition.khronos.opengles.GL10;

import android.content.res.AssetManager;

public class FontTexture extends GameTexture {
	// Stores the number for columns in the bitmap font
	private static final int COLUMNS = 16;
	// Stores the number of rows per font in the bitmap font
	private static final int ROWS = 8;
	// Stores the number of fonts in the bitmap font
	private static final int FONTS = 2;
	// Stores the margin around the characters
	private static final float VERTICAL_PADDING = 1.0f;
	// Stores the margin around the characters
	private static final float HORIZONTAL_PADDING = 1.0f;
	// Stores the Unicode value of the first character in the bitmap font
	private static final char FIRST_CHARACTER = 0x20; 

	public FontTexture(AssetManager assetManager, GL10 gl, String filename) {
		super(assetManager, gl, filename);
	}
	
	/**
	 * Must be between Unicode values of 0x20 and 0x9F to result in the correct position.
	 * 
	 * @param letter
	 * @return
	 */
	public float getX(char letter) {
		return getX(letter, Constants.DEFAULT_FONT);
	}
	
	/**
	 * Must be between Unicode values of 0x20 and 0x9F to result in the correct position.
	 * 
	 * @param letter
	 * @return
	 */
	public float getX(char letter, int font) {
		float padding = (width / COLUMNS - getCharacterWidth(letter, font)) / 2.0f;
		return (((letter - FIRST_CHARACTER) % COLUMNS) * width / COLUMNS + padding) / width;
		//return (((letter - FIRST_CHARACTER) % COLUMNS) * width / COLUMNS + HORIZONTAL_PADDING) / width;
	}
	
	/**
	 * 
	 * @param letter Must be between Unicode values of 0x20 and 0x9F to result in the correct position.
	 * @return
	 */
	public float getY(char letter) {
		return getY(letter, Constants.DEFAULT_FONT);
	}
	
	/**
	 * 
	 * 
	 * @param letter Must be between Unicode values of 0x20 and 0x9F to result in the correct position.
	 * @param font
	 * @return
	 */
	public float getY(char letter, int font) {
		return (((letter - FIRST_CHARACTER) / COLUMNS + ROWS * font) * (height / (ROWS * FONTS)) + VERTICAL_PADDING) / height;
	}
	
	public float getWidth(char letter) {
		return getWidth(letter, Constants.DEFAULT_FONT);
	}
	
	public float getWidth(char letter, int font) {
		return (getCharacterWidth(letter, font)) / width;
		//return (width / COLUMNS - 2 * HORIZONTAL_PADDING) / width;
	}
	
	public float getHeight() {
		return (height / (ROWS * FONTS) - 2 * VERTICAL_PADDING) / height;
	}

	private float getCharacterWidth(char letter, int font) {
		return Constants.FONT_METRICS[letter - FIRST_CHARACTER + font * ROWS * COLUMNS] + 2.0f * HORIZONTAL_PADDING;
	}
}
